import 'package:json_annotation/json_annotation.dart';

part 'account_model.g.dart';

/// 账户模型
/// 用于管理用户的各个账户（现金、银行卡、支付宝等）
@JsonSerializable()
class AccountModel {
  /// 账户ID
  final String id;

  /// 账户名称
  final String name;

  /// 账户类型（现金、银行卡、支付宝、微信等）
  final String type;

  /// 账户余额（单位：元）
  final double balance;

  /// 账户创建时间
  @JsonKey(name: 'created_at')
  final DateTime createdAt;

  /// 账户更新时间
  @JsonKey(name: 'updated_at')
  final DateTime updatedAt;

  /// 构造函数
  AccountModel({
    required this.id,
    required this.name,
    required this.type,
    required this.balance,
    required this.createdAt,
    required this.updatedAt,
  });

  /// 从JSON创建对象
  factory AccountModel.fromJson(Map<String, dynamic> json) =>
      _$AccountModelFromJson(json);

  /// 转换为JSON
  Map<String, dynamic> toJson() => _$AccountModelToJson(this);

  /// 复制并更新属性
  AccountModel copyWith({
    String? id,
    String? name,
    String? type,
    double? balance,
    DateTime? createdAt,
    DateTime? updatedAt,
  }) {
    return AccountModel(
      id: id ?? this.id,
      name: name ?? this.name,
      type: type ?? this.type,
      balance: balance ?? this.balance,
      createdAt: createdAt ?? this.createdAt,
      updatedAt: updatedAt ?? this.updatedAt,
    );
  }

  @override
  bool operator ==(Object other) =>
      identical(this, other) ||
      other is AccountModel &&
          runtimeType == other.runtimeType &&
          id == other.id;

  @override
  int get hashCode => id.hashCode;
}
